<template>
  <view class="cart-container" v-if='cart.length !== 0'>
    <!-- 收货地址区域 -->
    <my-address></my-address>

    <!-- 中间彩色分割线区域 -->
    <view>
      <image src="/static/cart_border@2x.png" class="color_border"></image>
    </view>

    <!-- 购物车标题 -->
    <view class="cart_title">
      <!-- 图标 -->
      <view class="icon">
        <uni-icons type='shop' size='18'></uni-icons>
      </view>
      <!-- 文本 -->
      <text>购物车</text>
    </view>

    <!-- 购物车商品列表 -->
    <!-- uni-swipe-action 是最外层包裹性质的容器： 实现滑动删除-->
    <uni-swipe-action>
      <view class="cart_list">
        <block v-for='(goods, i) in cart' :key='i'>
          <!-- uni-swipe-action-item 可以为其子节点提供滑动操作的效果。需要通过 options 属性来指定操作按钮的配置信息 -->
          <uni-swipe-action-item :right-options='options' @click='deleteGoodsHandler(goods)'>
            <my-goods :goods='goods' :showRadio='true' :showNum='true' :checked="goods.goods_state"
              @radio-change='radioChange' @num-change='numChange'></my-goods>
          </uni-swipe-action-item>
        </block>
      </view>
    </uni-swipe-action>
    
    <!-- 底部结算栏 -->
    <my-settlement></my-settlement>
  </view>
  
  <!-- 空的购物车区域 -->
  <view class="empty_cart" v-else>
    <image src="/static/cart_empty@2x.png" class="empty_img"></image>
    <text class="tips_text">空空如也~</text>
  </view>
</template>

<script>
  import {
    mapGetters,
    mapState,
    mapMutations
  } from 'vuex'
  import badgeMix from '@/mixins/tabbar-badge.js'

  export default {
    mixins: [badgeMix],
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#C00000'
          }
        }]
      }
    },
    computed: {
      ...mapState('m_cart', ['cart'])
    },
    methods: {
      ...mapMutations('m_cart', ['updateGoodsState', 'updateGoodsNum', 'deleteGoods']),
      radioChange(e) {
        // console.log(e)
        this.updateGoodsState(e)
      },
      numChange(e) {
        // console.log(e)
        this.updateGoodsNum(e)
      },
      deleteGoodsHandler(goods) {
        this.deleteGoods(goods)
      },
    }
  }
</script>

<style lang="scss">
  .cart-container {
    padding-bottom: 50px;
    .color_border {
      width: 100%;
      height: 5px;
      display: block;
    }

    .cart_title {
      height: 40px;
      font-size: 14px;
      // line-height: 40px;
      display: flex;
      align-items: center;
      border-bottom: 1px solid #efefef;
      padding-left: 5px;

      .icon {
        margin-right: 10px;
      }
    }
  }
  
  .empty_cart {
    padding-top: 150px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .empty_img {
      width: 90px;
      height: 90px;
    }
    .tips_text{
      color: gray;
      font-size: 12px;
      margin-top: 15px;
    }
  }
</style>
